package com.runner.player.state;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.runner.entity.CollisionEntity;
import com.runner.observer.Event;
import com.runner.player.Player;
import com.runner.utility.Locator;

public class RollState extends PlayerState {

	private float timer;
	public static float ROLL_TIME = 0.7f;
	private Vector2 posVec;
	private boolean jump;

	public RollState(Player p) {
		super(p);
	}

	@Override
	public void update(float delta) {
		player.getPhysics().getBody().setTransform(posVec, MathUtils.PI / 2);
		if (jump) {
			exit();
			player.changeState(new JumpState(player));
		}
		timer += delta;
		if (timer > ROLL_TIME) {
			exit();
			player.changeState(new NormalState(player));
		}
		
	}

	@Override
	public void newAction(INPUT input) {
		switch (input) {
		case JUMP:
			jump = true;
			break;
		case ROLL:
			timer=0;
			break;
		default:
			break;

		}
	}

	@Override
	public void enter() {
		jump = false;
		posVec = new Vector2(player.getPhysics().getBody().getPosition().x, player.getPhysics().getBody().getPosition().y - PLAYER_HEIGHT / 2
				* Locator.getySpriteScale());
		player.getSubject().notify(Event.EVENT_ROLL);
		player.setDirection(90);

		timer = 0;
	}

	@Override
	public void exit() {
		player.setDirection(0);
		player.getPhysics().getBody().setTransform(posVec, 0);
	}

	@Override
	public void collide(CollisionEntity otherBody) {
		if (otherBody.getPhysics().getType().equals(TYPE.GROUND)) {
			System.out.println("roll collide");
		}
	}

}
